const voListD = []
const myRemoveLayui = function (obj) {
    //加删除的id
    let item = $(obj).parent()
    if($(item).find("input[myid='four']").val()){
        voListD.push({
            chapterName: $(item).find("input[myid='one']").val(),
            videoUrl: $(item).find("input[myid='two']").val(),
            sort: $(item).find("input[myid='three']").val(),
            parentId: $('#userId').val(),
            id: $(item).find("input[myid='four']").val(),
            isDelete: '12'
        })
    }


    $(obj).parent().remove();
}
const addLayuiItems = () => {
    let str = `
						<div class="layui-form-item" style="display: flex;">
                            <div class="layui-inline"  style="width: calc((100% - 30px) / 3);">
                                 <label class="layui-form-label">子节名称</label>
                                 <div class="layui-input-block">
                                    <input class="form-control "  name="chapterName" myid="one"    type="text" placeholder="请输名称">
                                 </div>
                            </div>
                            <div class="layui-inline"  style="width: calc((100% - 30px) / 3);">
                                <label class="layui-form-label">视频链接</label>
                                <div class="layui-input-block">
                                 <input class="form-control "  myid="two"  type="text" placeholder="请输入视频链接">
                                </div>
                            </div>
                            <div class="layui-inline"  style="width: calc((100% - 30px) / 3);">
                                <label class="layui-form-label">优先级</label>
                                <div class="layui-input-block">
                                    <input class="form-control "  myid="three"  type="text" placeholder="请输入优先级" onkeyup="value=value.replace(/\D/g,'')">
                                    <input  myid="four" style="display: none">
                                </div>
                           </div>  
							<button  class="layui-inline layui-btn myremove1" type="button" onclick="myRemoveLayui(this)">删除</button>
                        </div>
	`
    $('#LayuiItems').append(str)
}

layui.use(['form', 'xmSelect', 'upload', 'laydate'], function () {
    var form = layui.form;
    var layer = layui.layer, laydate = layui.laydate;
    var xmSelect = layui.xmSelect, upload = layui.upload;
    var loading;
    let datas = [];

    //提交事件
    form.on('submit(submit)', function (data) {
        let params = data.field
        let mylayuiItems = $('#LayuiItems').find('.layui-form-item')

        if ($('#inputType').val() == 'addZ') {
            //新增章
            if ('' == params.chapterName) {
                layer.msg('请输入章节名称')
                return false
            }
            params.chapterName = $(mylayuiItems[0]).find("input[myid='one']").val()
            params.videoUrl = $(mylayuiItems[0]).find("input[myid='two']").val()
            params.sort = $(mylayuiItems[0]).find("input[myid='three']").val()
            params.parentId = $('#userId').val()
            AjaxPost('/jzweb/jazCoursesChapter/saveCourseChapter', params).then(function (response) {
                parent.layer.msg("操作成功");
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
                parent.tableReload($('#tableName').val());
            }).catch(error => {
                layer.msg(error.msg)
            })
        } else if ($('#inputType').val() == 'editZ'||$('#inputType').val() == 'editJ') {
            //编辑章节
            if ('' == params.chapterName) {
                layer.msg('请输入章节名称')
                return false
            }
            params.chapterName = $(mylayuiItems[0]).find("input[myid='one']").val()
            params.videoUrl = $(mylayuiItems[0]).find("input[myid='two']").val()
            params.sort = $(mylayuiItems[0]).find("input[myid='three']").val()
            params.id = $('#userId').val()
            AjaxPost('/jzweb/jazCoursesChapter/updateCourseChapter', params).then(function (response) {
                parent.layer.msg("操作成功");
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
                parent.tableReload($('#tableName').val());
            }).catch(error => {
                layer.msg(error.msg)
            })
        }else if($('#inputType').val() == 'addJ'){
            //新增节
            let voList = []
            for (let i = 0; i < mylayuiItems.length; i++) {
                let item = $(mylayuiItems[i])
                voList.push({
                    chapterName: $(item).find("input[myid='one']").val(),
                    videoUrl: $(item).find("input[myid='two']").val(),
                    sort: $(item).find("input[myid='three']").val(),
                    parentId: $('#userId').val()
                })
            }
             if(voListD.length>0){
                 params = [...voList,...voListD]
             }else{
                 params = [...voList]
             }
            AjaxPost('/jzweb/jazCoursesChapter/saveCourseChildChapter', params).then(function (response) {
                parent.layer.msg("操作成功");
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
                parent.tableReload($('#tableName').val());
            }).catch(error => {
                layer.msg(error.msg)
            })
        }

    });

});
